package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.service.network.BankRemoteConsole;
import com.acme.bankapp.service.network.BankServer;

/**
 * @author Artem Kalchenko
 * 
 */
public class SystemTest {

	private static Bank theBank = new Bank();
	private static BankService bs = new BankService();
	private static EmailService es = new EmailService();
	
	public static void main(String[] args) {
		if (args.length == 0) {
			//runMyTests();
			runEmailTest();
			return;
		}
		
		switch (args[0]) {
		case "-server":
			BankServer bankSrv = new BankServer();
			if ("-loadbank".equals(args[1])) {
				theBank = bs.loadBank(args[2]);
			} else {
				theBank = bs.loadBank();
			}
			bankSrv.run(theBank);
			break;
		case "-client":
			BankRemoteConsole bankCon = new BankRemoteConsole ("127.0.0.1", 6666);
			bankCon.startClientConsole();
			break;
		default:
			break;
		}
		
		if (args.length == 2) {
			if ("-loadbank".equals(args[0])) {
				theBank = bs.loadBank(args[1]);
				System.out.println("List of clients:\n"	+ bs.getClientsListAsString(theBank));
				System.out.println(bs.getAccountsListAsString(theBank));
			}
			if ("-loadfeed".equals(args[0])) {
				theBank = bs.loadBankFromFeed(args[1]);
				System.out.println("List of clients:\n"	+ bs.getClientsListAsString(theBank));
				System.out.println(bs.getAccountsListAsString(theBank));
			}
		}
	}

	private static void runEmailTest() {
		es.start();
		for (int i=0; i<10; i++) {
			es.addEmailToQueue(new Email("Client "+i, "", "", ""));
			System.out.println("Email " + i + " added");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		es.stop();
	}

	private static void runMyTests() {
		Client clSmith = new Client("Smith", Gender.MALE);
		Client clLiskov = new Client("Liskov", Gender.FEMALE,
				new CheckingAccount(500, 100));
		Client clSmithCopy = null;

		bs.saveBank(theBank);
		
		clSmith = bs.addClient(theBank, clSmith);
		clLiskov = bs.addClient(theBank, clLiskov);

		System.out.println("List of clients:\n"
				+ bs.getClientsListAsString(theBank));
		System.out.println(bs.getAccountsListAsString(theBank));

		// add an account for Smith
		bs.addAccount(theBank, clSmith, new SavingsAccount(750));
		System.out.println(bs.getAccountsListAsString(theBank));

		theBank = bs.loadBank();
		if (theBank != null) {
			System.out.println(bs.getAccountsListAsString(theBank));
		}
	}
}
